home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / include / oldcomplex.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-16  |  10.4 KB  |  430 lines

  1. /*ident    "@(#)oldcomplex.h    1.3   6/2/90" */
  2. #if __DMC__ || __RCC__
  3. #pragma once
  4. #endif
  5.  
  6. #ifndef __OLDCOMPLEX_H
  7. #define __OLDCOMPLEX_H 1
  8.  
  9. #ifndef __cplusplus
  10. #error  Use C++ compiler for oldcomplex.h
  11. #endif
  12. /*
  13. **  (c) Copyright 1988-1990 by Dyad Software Corp.
  14. **  All Rights Reserved
  15. **
  16. **  Authors: lwd, geb
  17. */
  18.  
  19. #include <math.h>
  20. #include <fltpnt.h>
  21. #undef sqrt
  22.  
  23. class ostream;
  24. class istream;
  25.  
  26.  
  27. class complex
  28. {
  29.  
  30.  public:
  31.    friend double real ( const complex& );     
  32.    friend double imag ( const complex& );     
  33.  
  34.    friend complex cos ( const complex& );
  35.    friend complex cosh ( const complex& );
  36.    friend complex sin ( const complex& );
  37.    friend complex sinh ( const complex& );
  38.    friend complex tan ( const complex& );
  39.    friend complex tanh ( const complex& );
  40.    friend complex log ( const complex& );
  41.    friend complex log10 ( const complex& );
  42.    friend complex sqrt ( const complex& );
  43.    friend double abs ( const complex& );
  44.    friend complex conj ( const complex& );
  45.    friend double norm ( const complex& );
  46.    friend double modulus ( const complex& );
  47.    friend double arg ( const complex& );
  48.    friend complex asin ( const complex& );
  49.    friend complex acos ( const complex& );
  50.    friend complex atan ( const complex& );
  51.    friend complex asinh ( const complex& );
  52.    friend complex atanh ( const complex& );
  53.    friend complex exp ( const complex& );
  54.    friend complex polar ( double r, double theta = 0);
  55.  
  56.    friend complex operator + ( const complex&, const complex& );
  57.    friend complex operator + ( double, const complex& );
  58.    friend complex operator + ( const complex&, double );
  59.  
  60.    friend complex operator - ( const complex&, const complex& );
  61.    friend complex operator - ( double, const complex& );
  62.    friend complex operator - ( const complex&, double );
  63.  
  64.    friend complex operator * ( const complex&, const complex& );
  65.    friend complex operator * ( double, const complex& );
  66.    friend complex operator * ( const complex&, double );
  67.  
  68.    friend complex operator / ( const complex&, const complex& );
  69.    friend complex operator / ( double, const complex& );
  70.    friend complex operator / ( const complex&, double );
  71.  
  72.    friend complex pow( const complex& lhs, double rhs );
  73.    friend complex pow( const complex& lhs, const complex & rhs );
  74.    friend complex pow( double lhs, const complex & rhs);
  75.    friend complex pow( const complex& lhs, int rhs);
  76.  
  77.    friend int operator && ( const complex&, const complex& );
  78.    friend int operator && ( double, const complex& );
  79.    friend int operator && ( const complex&, double );
  80.  
  81.    friend int operator || ( const complex&, const complex& );
  82.    friend int operator || ( double, const complex& );
  83.    friend int operator || ( const complex&, double );
  84.  
  85.    friend int operator != ( const complex&, const complex& );
  86.    friend int operator != ( double, const complex& );
  87.    friend int operator != ( const complex&, double );
  88.  
  89.    friend int operator == ( const complex&, const complex& );
  90.    friend int operator == ( double, const complex& );
  91.    friend int operator == ( const complex&, double );
  92.  
  93.    friend ostream& operator << ( ostream& s, const complex& x);
  94.    friend istream& operator >> ( istream& s, complex& x);
  95.  
  96.  public:
  97.  
  98.    complex ( );
  99.    complex ( double r, double i = 0 ); 
  100.    complex ( const complex & );   
  101.    double& real ( );
  102.    double& imag ( );
  103.    
  104.    complex& operator = ( const complex& );
  105.    complex& operator = ( double );
  106.    
  107.    complex& operator += ( const complex& );
  108.    complex& operator += ( double );
  109.    
  110.    complex& operator -= ( const complex& );
  111.    complex& operator -= ( double );
  112.    
  113.    complex& operator *= ( const complex& );
  114.    complex& operator *= ( double );
  115.    
  116.    complex& operator /= ( const complex& );
  117.    complex& operator /= ( double );
  118.    
  119.    int operator ! () const;
  120.    complex operator - () const;
  121.    
  122.  private:
  123.    double re;
  124.    double im;
  125.  };
  126.  
  127. inline complex::complex ( )
  128.     re = im = NAN;
  129. }
  130.  
  131. inline complex::complex ( double r, double i ): re(r), im(i) {}
  132.      inline complex::complex( const complex& z)
  133. {
  134.   re = z.re;
  135.   im = z.im;
  136. }    
  137.  
  138. inline double real ( const complex& z){ return z.re; }     
  139. inline double imag ( const complex& z){ return z.im; }
  140.      
  141. inline double& complex::real (){ return re; }     
  142. inline double& complex::imag (){ return im; }
  143.      
  144. /* operator + */
  145. inline complex operator + ( const complex& lhs, const complex& rhs )
  146. {
  147.   return  complex ( lhs.re + rhs.re, lhs.im + rhs.im );
  148. }
  149. inline complex operator + ( double  lhs, const complex& rhs )
  150. {
  151.   return  complex ( lhs + rhs.re, rhs.im );
  152. }
  153. inline complex operator + ( const complex& lhs, double rhs )
  154. {
  155.   return  complex ( lhs.re + rhs, lhs.im );
  156. }
  157.  
  158. /* operator - */
  159. inline complex operator - ( const complex& lhs, const complex& rhs )
  160. {
  161.   return  complex ( lhs.re - rhs.re, lhs.im - rhs.im );
  162. }
  163. inline complex operator - ( double lhs, const complex& rhs )
  164. {
  165.   return  complex ( lhs - rhs.re, - rhs.im );
  166. }
  167. inline complex operator - ( const complex& lhs, double rhs )
  168. {
  169.   return  complex ( lhs.re - rhs, lhs.im );
  170. }
  171.  
  172. /* operator * */
  173. inline complex operator * ( const complex& lhs, const complex& rhs )
  174. {
  175.   return  complex ( lhs.re * rhs.re - lhs.im * rhs.im, 
  176.            lhs.re * rhs.im + lhs.im*rhs.re );
  177. }
  178. inline complex operator * ( double lhs, const complex& rhs )
  179. {
  180.   return  complex ( lhs * rhs.re, lhs * rhs.im );
  181. }
  182. inline complex operator * ( const complex& lhs, double rhs )
  183. {
  184.   return  complex ( lhs.re * rhs, lhs.im*rhs );
  185. }
  186.  
  187. /* operator / */
  188.  
  189. inline complex operator / ( const complex& lhs, double rhs )
  190. {
  191.   return complex( lhs.re / rhs, lhs.im /rhs );
  192. }
  193.  
  194. /* unary operators */
  195. inline complex complex::operator - ( ) const
  196. {
  197.   return  complex ( -re, -im );
  198. }
  199.  
  200. inline int complex::operator ! ( )  const
  201. {
  202.   return  ( re == 0 ) && ( im == 0 ) ;
  203. }    
  204.  
  205.  
  206. inline complex & complex::operator = ( const complex& z)
  207. {
  208.   re = z.re;
  209.   im = z.im;
  210.   return  *this;
  211. }    
  212.  
  213. inline complex & complex::operator = ( double x)
  214. {
  215.   re = x;
  216.   im = 0;
  217.   return  *this;
  218. }    
  219.  
  220. /* operator *= */
  221. inline complex & complex::operator *= ( const complex& z)
  222. {
  223.   *this = *this * z;
  224.   return  *this;
  225. }
  226. inline complex & complex::operator *= ( double x )
  227. {
  228.   re *= x;
  229.   im *= x;
  230.   return  *this;
  231. }
  232.  
  233. inline complex & complex::operator /= ( const complex& z)
  234. {
  235.   *this = *this / z;
  236.   return  *this;
  237. }    
  238. inline complex & complex::operator /= ( double x)
  239. {
  240.   re /= x;
  241.   im /= x;
  242.   return  *this;
  243. }    
  244.  
  245. inline complex & complex::operator += ( const complex& z)
  246. {
  247.   re += z.re;
  248.   im += z.im;
  249.   return  *this;
  250. }
  251. inline complex & complex::operator += ( double x)
  252. {
  253.   re += x;
  254.   return  *this;
  255. }
  256.  
  257. inline complex & complex::operator -= ( const complex& z)
  258. {
  259.   re -= z.re;
  260.   im -= z.im;
  261.   return  *this;
  262. }
  263.  
  264. inline complex & complex::operator -= ( double x)
  265. {
  266.   re -= x;
  267.   return  *this;
  268. }    
  269.  
  270. /* operator && */
  271. inline int operator && ( const complex & lhs, const complex & rhs )
  272. {
  273.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) &&
  274.     (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  275. }    
  276. inline int operator && ( double lhs, const complex & rhs )
  277. {
  278.   return  ( lhs != 0 ) && (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  279. }    
  280. inline int operator && ( const complex & lhs, double rhs )
  281. {
  282.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) && ( rhs != 0 ) ;
  283. }    
  284.  
  285. /* operator || */
  286. inline int operator || ( const complex & lhs, const complex & rhs )
  287. {
  288.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) ||
  289.     (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  290. }
  291. inline int operator || ( double lhs, const complex & rhs )
  292. {
  293.   return  ( lhs != 0 ) || (( rhs.re != 0 ) || ( rhs.im != 0 )) ;
  294. }
  295. inline int operator || ( const complex & lhs, double rhs )
  296. {
  297.   return  (( lhs.re != 0 ) || ( lhs.im != 0 )) || ( rhs != 0 );
  298. }    
  299.  
  300. /* operator != */
  301. inline  int operator != ( const complex& lhs, const complex& rhs )
  302. {
  303.   return  ( lhs.re != rhs.re ) || ( lhs.im != rhs.im );
  304. }
  305. inline  int operator != ( double lhs, const complex& rhs )
  306. {
  307.   return  ( lhs != rhs.re ) || ( 0 != rhs.im );
  308. }
  309. inline  int operator != ( const complex& lhs, double rhs )
  310. {
  311.   return  ( lhs.re != rhs ) || ( lhs.im != 0 );
  312. }
  313.  
  314. /* operator == */
  315. inline  int operator == ( const complex& lhs, const complex& rhs )
  316. {
  317.   return  ( lhs.re == rhs.re ) && ( lhs.im == rhs.im );
  318. }
  319. inline  int operator == ( double lhs, const complex& rhs )
  320. {
  321.   return  ( lhs == rhs.re ) && ( 0 == rhs.im );
  322. }
  323. inline  int operator == ( const complex& lhs, double rhs )
  324. {
  325.   return  ( lhs.re == rhs ) && ( lhs.im == 0 );
  326. }
  327.  
  328. inline double arg( const complex& z)
  329. {
  330.   return atan2(z.im,z.re);
  331. }
  332.  
  333. inline complex atanh( const complex& z) 
  334. {
  335.   complex iz(-z.im, z.re);
  336.   complex ix = atan(iz);
  337.   return complex ( ix.im, -ix.re);
  338. }
  339.  
  340. inline complex asinh( const complex& z)
  341. {
  342.   complex iz(-z.im, z.re);
  343.   complex ix = asin(iz);
  344.   return complex ( ix.im, -ix.re);
  345. }
  346.  
  347. inline double norm( const complex& z)
  348. {
  349.   return z.re*z.re + z.im*z.im;
  350. }
  351.  
  352. inline double modulus( const complex& z)
  353. {
  354.   return abs(z);
  355. }
  356.  
  357. inline complex conj( const complex& z)
  358. {
  359.   return complex( z.re, -z.im );
  360. }
  361.  
  362. inline complex cos( const complex& z )
  363. {
  364.   return complex( cos(z.re) * cosh( z.im ),  -( sin( z.re) * sinh(z.im)));
  365. }
  366.  
  367. inline complex cosh( const complex& z )
  368. {
  369.   return complex ( cosh(z.re) * cos(z.im), sinh(z.re) * sin(z.im) );
  370. }
  371.  
  372. inline complex sin( const complex& z )
  373. {
  374.   return complex ( sin(z.re) * cosh(z.im), cos(z.re) * sinh(z.im) );
  375. }
  376.  
  377. inline complex sinh( const complex& z )
  378. {
  379.   return complex ( sinh(z.re) * cos(z.im), cosh(z.re) * sin(z.im) );
  380. }
  381.  
  382. inline complex tan( const complex& z )
  383. {
  384.   double x = 2*z.re;
  385.   double y = 2*z.im;
  386.   double t = 1.0/(cos(x) +cosh(y));
  387.   
  388.   return complex( t*sin(x), t*sinh(y) );
  389. }
  390.  
  391. inline complex tanh( const complex& z )
  392. {
  393.   double x = 2*z.re;
  394.   double y = 2*z.im;
  395.   double t = 1.0/(cosh(x) +cos(y));
  396.   
  397.   return complex( t*sinh(x), t*sin(y) );
  398. }    
  399.  
  400. inline complex exp( const complex& z )
  401. {
  402.   double x = exp(z.re);
  403.   return complex( x*cos(z.im), x*sin(z.im) );
  404. }    
  405.  
  406. inline complex log( const complex& z )
  407. {
  408.   return complex( log( abs(z) ), arg( z ) );
  409. }    
  410.  
  411. inline complex log10( const complex& z )
  412. {
  413.   return complex( 0.2171472409516259*log( norm(z) ), arg( z ) );
  414. }    
  415.  
  416. inline complex polar ( double r, double theta )
  417. {
  418.   return complex ( r * cos(theta), r * sin(theta));
  419. }
  420.  
  421.  
  422.  
  423. #endif
  424.  
  425.  
  426.  
  427.  
  428.  
  429.